//
//  MZMjfresh.swift
//  MZTool_Example
//
//  Created by gby on 2023/1/29.
//  Copyright © 2023 CocoaPods. All rights reserved.
//

import Foundation


public enum NMMJHeaderType {
    case normal
    case customNormal
}

public enum NMMJFooterType {
    case normal
}

import Foundation
import NVActivityIndicatorView
import MJRefresh
import MZTool

//MARK:自定义刷新
public final class MZMJrefresh {

    public static func mjHeader(_ type: NMMJHeaderType = .normal, hander: @escaping ()->() ) -> MJRefreshHeader? {
        switch type {
        case .normal:
            let header = MJRefreshNormalHeader.init(refreshingBlock: hander)
            header.stateLabel?.font = UIFont.mz.size(12)
            header.stateLabel?.textColor = MZ.RGB("#333333")
            header.setTitle(String.headerIdle, for: .idle)
            header.setTitle(String.headerPulling, for: .pulling)
            header.setTitle(String.headerRefreshing, for: .refreshing)
            header.lastUpdatedTimeLabel?.font = header.stateLabel?.font
            header.lastUpdatedTimeLabel?.textColor = header.stateLabel?.textColor
            return header
        case .customNormal:
            let header = CustomMjHeader.init(refreshingBlock: hander)
            return header
        }
    }
    
    public static func mjFooter(_ type: NMMJFooterType = .normal, hander: @escaping ()->() ) -> MJRefreshFooter? {
        let footer = MJRefreshBackStateFooter.init(refreshingBlock: hander)
        footer.stateLabel?.font = UIFont.mz.size(14)
        footer.stateLabel?.textColor = UIColor.black
        footer.setTitle(String.footerNomore, for: .noMoreData)
        return footer
    }
}


fileprivate extension String {
    static let headerIdle = NSLocalizedString("下拉刷新", comment: "")
    static let headerPulling = NSLocalizedString("释放刷新", comment: "")
    static let headerRefreshing = NSLocalizedString(" 加载中", comment: "")
    static let footerNomore = NSLocalizedString("我也是有底线的", comment: "没有更多数据时候")
}


//MARK:自定义下拉刷新
final class CustomMjHeader: MJRefreshHeader{

    var freshText: UILabel = UILabel.init(12, color:MZ.RGB(102, 102, 102, 1),aligment: .left)
    var arrowImgv: UIImageView = UIImageView.init(image: MZ.IMG("mjrefresh_arrow"))
    var activityIndicatorView: NVActivityIndicatorView = NVActivityIndicatorView.init(frame: CGRect(x: 0, y: 0, width: 18, height: 18),
                                                                                      type: .ballRotate,//.ballClipRotatePulse,
                                                                                      color: MZ.RGB(102, 102, 102, 1),
                                                                                      padding: 0)

    override func prepare() {
        super.prepare()

        self.addSubview(freshText)
        self.addSubview(arrowImgv)
        self.addSubview(activityIndicatorView)
    }

    override func placeSubviews() {
        super.placeSubviews()

        freshText.frame = CGRect(x: 0, y: 0, width: 80, height: freshText.font.lineHeight);
        freshText.center = CGPoint(x: mj_w * 0.5 + 40 - 22, y: 35);

        arrowImgv.frame = CGRect(x: 0, y: 0, width: 6, height: 16);
        arrowImgv.center = CGPoint(x: mj_w * 0.5 - 32, y: 35);

        activityIndicatorView.center = CGPoint(x: mj_w * 0.5 - 34, y: 35);
    }

    override func scrollViewContentSizeDidChange(_ change: [AnyHashable : Any]?) {
        super.scrollViewContentSizeDidChange(change)
    }

    override func scrollViewPanStateDidChange(_ change: [AnyHashable : Any]?) {
        super.scrollViewPanStateDidChange(change)
    }

    override func scrollViewContentOffsetDidChange(_ change: [AnyHashable : Any]?) {
        super.scrollViewContentOffsetDidChange(change)
    }

    override var state: MJRefreshState {
        willSet{

        }
        didSet{
            guard oldValue != state else {
                return
            }
            arrowImgv.isHidden = false
            activityIndicatorView.isHidden = true
            activityIndicatorView.stopAnimating()
            switch state {
            case .idle:
                freshText.text = String.headerIdle
                arrowImgv.transform = CGAffineTransform.identity
                break
            case .pulling:
                freshText.text = String.headerPulling
                arrowImgv.transform = CGAffineTransform.init(rotationAngle: .pi)
                break
            case .refreshing:
                freshText.text = String.headerRefreshing
                arrowImgv.isHidden = true
                activityIndicatorView.isHidden = false
                activityIndicatorView.startAnimating()
                break
            default:
                break
            }
        }
    }
}
